// index.js
import http from "../../utils/http"
import Toast from '@vant/weapp/toast/toast';

Page({
    data: {
        msg: 'http://124.223.9.151:9009/static/logo.png',
       form:{
        username: '201711010001',
        password: '',
        userType: 1,
       },
       usernameMsg:'',
       passwordMsg:''
    },
    usernameInput(e) {
        console.log(e);
        this.setData({
            "form.username": e.detail
        })
    },
    passwordInput(e) {
        this.setData({
            "form.password": e.detail
        })
    },
    onSubmit() {
        console.log(this.data.form);
        if (this.validate()) {
            http({
                url: '/user/login',
                method: "post",
                data: this.data.form
            }).then(res => {
                console.log(res);
                if (res.code === 0) {
                    Toast({
                        type: 'success',
                        message: '登录成功',
                        onClose: () => {
                            console.log('执行close函数');
                            wx.setStorageSync('userInfo', res.data)
                            wx.switchTab({
                                url: "/pages/logs/logs",
                            })
                        }
                    })
                }else{
                    Toast.fail(res.message)
                }
            }).catch(err=>{
                console.log(err);
                Toast.fail("失败")
            })
        }
    },
    validate(){
        let v=true;
        if (!this.data.form.username) {
            v=false
            this.setData({
                usernameMsg:"请填写账号"
            })
        } else {
            this.setData({
                usernameMsg:''
            })
        }
        if (!this.data.form.password) {
            v=false
            this.setData({
                passwordMsg:"请填写密码"
            })
        } else {
            this.setData({
                passwordMsg:''
            })
        }
        return v
    },
    bindViewTap() {

    },
    onChooseAvatar(e) {

    },
    onInputChange(e) {},
    getUserProfile(e) {},
})